package com.efesco.system;

import com.bstek.dorado.common.*;
import com.bstek.dorado.data.*;
import com.bstek.dorado.data.db.DBStatement;
import com.bstek.dorado.data.db.SqlDataset;
import com.bstek.dorado.view.*;
import com.bstek.dorado.view.data.*;
import com.bstek.dorado.view.control.*;
import com.efesco.common.DBTools;
import com.efesco.common.Tools;

/**
 * employeeRoleViewModel
 */
public class employeeRoleViewModel extends DefaultViewModel {
	public void saveRole(ParameterSet parameters) throws Exception {
		Dataset employeeRoleDataset = getDataset("employeeRoleDataset");
		DBStatement statement = new DBStatement();
		employeeRoleDataset.moveFirst();
		while (!employeeRoleDataset.isLast()) {
			if (Tools.toStr(employeeRoleDataset.getString("FE_EMP_ROLE_ID")).equals("")) {
				String feEmployeeRoleId = DBTools.getNewId("general");
				employeeRoleDataset.setValue("FE_EMP_ROLE_ID", feEmployeeRoleId);
				statement.setSql("insert into s_fe_emp_role(fe_emp_role_id,role_ins_id,employee_id) values("
  				    +feEmployeeRoleId+","+Tools.toStr(employeeRoleDataset.getString("ROLE_INS_ID"))+","
				    +Tools.toStr(employeeRoleDataset.getString("EMPLOYEE_ID"))+")");
				statement.execute();
				statement.close();
			}

			String roleInsId=Tools.toStr(employeeRoleDataset.getString("ROLE_INS_ID"));
			String employeeId=Tools.toStr(employeeRoleDataset.getString("EMPLOYEE_ID"));
			
			SqlDataset userDataset = new SqlDataset();
			userDataset.setSql("select user_id from s_user where for_employee_id="+employeeId);
			userDataset.load(true);
			userDataset.moveFirst();
			String userId=userDataset.getString("user_id");
			
			SqlDataset functionTemplateDataset = new SqlDataset();
			functionTemplateDataset.setSql("select fr.function_id from s_function_template ft,s_function_template_right fr " +
					"where ft.function_template_id=fr.function_template_id and ft.role_type="+roleInsId);
			functionTemplateDataset.load(true);
			while (!functionTemplateDataset.isLast())  {
				statement.setSql("delete from s_function_right where function_id="
						+functionTemplateDataset.getString("function_id")+
						" and user_id = "+userId);
				statement.execute();
				statement.close();
				statement.setSql("insert into s_function_right(function_id,user_id) values ("
						+functionTemplateDataset.getString("function_id")+
						","+userId+")");
				statement.execute();
				statement.close();
				
				functionTemplateDataset.moveNext();
			}
			
			employeeRoleDataset.moveNext();
		}
		
			
	}
	
	public void deleteRole(ParameterSet parameters) throws Exception {
		Dataset employeeRoleDataset = getDataset("employeeRoleDataset");
		DBStatement statement = new DBStatement();
		employeeRoleDataset.moveFirst();
		while (!employeeRoleDataset.isLast()) {
			String roleInsId=Tools.toStr(employeeRoleDataset.getString("ROLE_INS_ID"));
			String employeeId=Tools.toStr(employeeRoleDataset.getString("EMPLOYEE_ID"));
			String employeeRoleId=Tools.toStr(employeeRoleDataset.getString("FE_EMP_ROLE_ID"));
			
			SqlDataset userDataset = new SqlDataset();
			userDataset.setSql("select user_id from s_user where for_employee_id="+employeeId);
			userDataset.load(true);
			userDataset.moveFirst();
			String userId=userDataset.getString("user_id");
			
			SqlDataset functionTemplateDataset = new SqlDataset();
			functionTemplateDataset.setSql("select fr.function_id from s_function_template ft,s_function_template_right fr " +
					"where ft.function_template_id=fr.function_template_id and ft.role_type="+roleInsId);
			functionTemplateDataset.load(true);
			while (!functionTemplateDataset.isLast())  {
				statement.setSql("delete from s_function_right where function_id="
						+functionTemplateDataset.getString("function_id")+
						" and user_id = "+userId);
				statement.execute();
				statement.close();
				functionTemplateDataset.moveNext();
			}
			
			statement.setSql("delete from s_fe_emp_role where FE_EMP_ROLE_ID="
					+employeeRoleId);
			statement.execute();
			statement.close();
			employeeRoleDataset.moveNext();
		}
		
			
	}

}
